Option Explicit Off
Option Strict Off
Public Class DatosSQLServer
    Inherits Acceso_a_Datos.Datos.gDatos
    Private Shared mInstance As DatosSQLServer
    Private Shared mMutex As New System.Threading.Mutex()
    Public Overrides Property CadenaConexion() As String
        Get
            If Len(MyBase.mCadenaConexion) = 0 Then
                Me.mdsRDBMS.ReadXml(gDatos.projectPath)
                mCadenaConexion = Me.mdsRDBMS.dt_CadenasdeConeccion.Select("rdbm_Nombre ='MSSqlServer'")(0).Item("rdbms_CadenaConeccion").ToString
            End If
            Return mCadenaConexion
        End Get
        Set(ByVal value As String)
            mCadenaConexion = value
        End Set
    End Property

    Protected Overrides Sub CargarParametros(ByVal Comando As System.Data.IDbCommand, ByVal Args() As Object)
        
        Dim i As Integer
        With Comando
            For i = 0 To Args.GetUpperBound(0)
                Try
                    CType(.Parameters(i + 1), System.Data.SqlClient.SqlParameter).Value = Args(i) 'aca hace eso de la cajita porque estaban de tipo object
                Catch Qex As Exception
                    Throw (Qex)
                End Try
            Next
        End With

    End Sub
    Shared mColComandos As New System.Collections.Hashtable()
    Protected Overrides Function Comando(ByVal ProcedimientoAlmacenado As String) As System.Data.IDbCommand
        Dim mComando As System.Data.SqlClient.SqlCommand
        If mColComandos.Contains(ProcedimientoAlmacenado) Then
            mComando = _
               CType(mColComandos.Item(ProcedimientoAlmacenado) _
            , System.Data.SqlClient.SqlCommand)
        Else
            Dim oConexion2 As New System.Data.SqlClient.SqlConnection( _
               CadenaConexion)
            oConexion2.Open()
            mComando = New _
               System.Data.SqlClient.SqlCommand( _
               ProcedimientoAlmacenado, oConexion2)
            
            mComando.CommandType = CommandType.StoredProcedure
            SqlClient.SqlCommandBuilder.DeriveParameters(mComando)
            oConexion2.Close()
            Try
                mColComandos.Add(ProcedimientoAlmacenado, mComando)
            Catch
            End Try
        End If
        With mComando
            .Connection = Me.Conexion
            .Transaction = MyBase.mTransaccion
        End With
        Return mComando

    End Function

    Protected Overrides Function CrearConexion(ByVal Cadena As String) As System.Data.IDbConnection
        Return New System.Data.SqlClient.SqlConnection(Cadena)
    End Function

    Protected Overrides Function CrearDataAdapter(ByVal ProcedimientoAlmacenado As String, ByVal ParamArray Args() As Object) As System.Data.IDataAdapter
        Dim mCom As System.Data.SqlClient.SqlCommand = _
           Comando(ProcedimientoAlmacenado)
        ' Si se han recibido Argumentos, 
        'se procede a asignar los valores correspondientes
        If Args IsNot Nothing Then
            CargarParametros(mCom, Args)
        End If
        Return New System.Data.SqlClient.SqlDataAdapter(mCom)

    End Function
#Region "Constructores"
    Sub New()

    End Sub
    Private Sub New(ByVal CadenaConexion As String)
        Me.New()
        Me.CadenaConexion = CadenaConexion
    End Sub
    Private Sub New(ByVal Servidor As String, ByVal Base As String)
        Me.New()
        Me.Base = Base
        Me.Servidor = Servidor
    End Sub
    Public Overloads Shared Function traerInstancia(ByVal servidor As String, ByVal base As String) As DatosSQLServer
        mMutex.WaitOne()
        If mInstance Is Nothing Then
            mInstance = New DatosSQLServer(servidor, base)
        End If
        mMutex.ReleaseMutex()
        Return mInstance
    End Function
    Public Overloads Shared Function traerInstancia() As DatosSQLServer
        mMutex.WaitOne()
        If mInstance Is Nothing Then
            mInstance = New DatosSQLServer()
        End If
        mMutex.ReleaseMutex()
        Return mInstance
    End Function

#End Region

End Class
